package com.easytrain.app;

import com.easytrain.app.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONObject;

public class ListViewPriceAdapter extends BaseAdapter{
	private Context 					context;
	private JSONArray 					listItems;
	private LayoutInflater 				listContainer;
	private int 						itemViewResource;
	static class ListItemView{				
		public TextView No;
		public TextView trainNo;
		public TextView startStation;
		public TextView arriveStation;
		public TextView businessSeat;
		public TextView noZeroSeat;
		public TextView noOneSeat;
		public TextView noSecondSeat;
		public TextView businessSleepSoftSeat;
		public TextView softSleepSeat;
		public TextView hardSleepSeat;
		public TextView softSeat;
		public TextView startTime;
		public TextView arriveTime;
		public TextView costTime;
		public TextView beginStation;
		public TextView endStation;
		public TextView trainType;
		public TextView airCondition;
	 }  

	public ListViewPriceAdapter(Context context, JSONArray data,int resource) {
		this.context = context;		
		this.listContainer = LayoutInflater.from(context);
		this.itemViewResource = resource;
		this.listItems = data;
	}
	
	public int getCount() {
		return listItems.length();
	}

	public Object getItem(int arg0) {
		return null;
	}

	public long getItemId(int arg0) {
		return 0;
	}
	
	public View getView(int position, View convertView, ViewGroup parent) {
		ListItemView  listItemView = null;
		
		if (convertView == null) {
			convertView = listContainer.inflate(this.itemViewResource, null);
			
			listItemView = new ListItemView();

			listItemView.No = (TextView)convertView.findViewById(R.id.No);
			listItemView.trainNo = (TextView)convertView.findViewById(R.id.trainNo);
			listItemView.startStation = (TextView)convertView.findViewById(R.id.startStation);
			listItemView.arriveStation = (TextView)convertView.findViewById(R.id.arriveStation);
			listItemView.businessSeat = (TextView)convertView.findViewById(R.id.businessSeat);
			listItemView.noZeroSeat = (TextView)convertView.findViewById(R.id.noZeroSeat);
			listItemView.noOneSeat = (TextView)convertView.findViewById(R.id.noOneSeat);
			listItemView.noSecondSeat = (TextView)convertView.findViewById(R.id.noSecondSeat);
			listItemView.businessSleepSoftSeat = (TextView)convertView.findViewById(R.id.businessSleepSoftSeat);
			listItemView.softSleepSeat = (TextView)convertView.findViewById(R.id.softSleepSeat);
			listItemView.hardSleepSeat = (TextView)convertView.findViewById(R.id.hardSleepSeat);
			listItemView.softSeat = (TextView)convertView.findViewById(R.id.softSeat);
			listItemView.startTime = (TextView)convertView.findViewById(R.id.startTime);
			listItemView.arriveTime = (TextView)convertView.findViewById(R.id.arriveTime);
			listItemView.costTime = (TextView)convertView.findViewById(R.id.costTime);
			listItemView.beginStation = (TextView)convertView.findViewById(R.id.beginStation);
			listItemView.endStation = (TextView)convertView.findViewById(R.id.endStation);
			listItemView.trainType = (TextView)convertView.findViewById(R.id.trainType);
			listItemView.airCondition = (TextView)convertView.findViewById(R.id.airCondition);
			
			convertView.setTag(listItemView);
		}else {
			listItemView = (ListItemView)convertView.getTag();
		}	
		try{
			JSONObject item = listItems.getJSONObject(position);		
			listItemView.No.setText(item.getString("No"));
			listItemView.trainNo.setText(item.getString("trainNo"));
			listItemView.startStation.setText(item.getString("startStation"));
			listItemView.arriveStation.setText(item.getString("arriveStation"));
			listItemView.businessSeat.setText(item.getString("businessSeat"));
			listItemView.noZeroSeat.setText(item.getString("noZeroSeat"));
			listItemView.noOneSeat.setText(item.getString("noOneSeat"));
			listItemView.noSecondSeat.setText(item.getString("noSecondSeat"));
			listItemView.businessSleepSoftSeat.setText(item.getString("businessSleepSoftSeat"));
			listItemView.softSleepSeat.setText(item.getString("softSleepSeat"));
			listItemView.hardSleepSeat.setText(item.getString("hardSleepSeat"));
			listItemView.softSeat.setText(item.getString("softSeat"));
			listItemView.startTime.setText(item.getString("startTime"));
			listItemView.arriveTime.setText(item.getString("arriveTime"));
			listItemView.costTime.setText(item.getString("costTime"));
			listItemView.beginStation.setText(item.getString("beginStation"));
			listItemView.endStation.setText(item.getString("endStation"));
			listItemView.trainType.setText(item.getString("trainType"));
			listItemView.airCondition.setText(item.getString("airCondition"));
			
		}catch(Exception ex){
			
		}
		return convertView;
	}
}
